package com.prs.crm.action.stock;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.stock.StockAlert;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StockAlertService;

public class StockAlertAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8265105071799192554L;
	private Collection<StockAlert> stockAlerts;
	private StockAlert stockAlert;
	private StockAlertService stockAlertService;
	private PaginationSupport pagination;
	private Product product;
	private StoreRoom storeRoom;
	private Collection<ProductStore> productStores;
	private ProductStoreService productStoreService;
	private ProductStore productStore;
	private String codes;
	private  List Nums;
	
	public Collection<ProductStore> getProductStores() {
		return productStores;
	}

	public void setProductStores(Collection<ProductStore> productStores) {
		this.productStores = productStores;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public StoreRoom getStoreRoom() {
		return storeRoom;
	}

	public void setStoreRoom(StoreRoom storeRoom) {
		this.storeRoom = storeRoom;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Collection<StockAlert> getStockAlerts() {
		return stockAlerts;
	}

	public void setStockAlerts(Collection<StockAlert> stockAlerts) {
		this.stockAlerts = stockAlerts;
	}

	public StockAlert getStockAlert() {
		return stockAlert;
	}

	public void setStockAlert(StockAlert stockAlert) {
		this.stockAlert = stockAlert;
	}

	public StockAlertService getStockAlertService() {
		return stockAlertService;
	}

	public void setStockAlertService(StockAlertService stockAlertService) {
		this.stockAlertService = stockAlertService;
	}

	public ProductStore getProductStore() {
		return productStore;
	}

	public void setProductStore(ProductStore productStore) {
		this.productStore = productStore;
	}

	@Action("listStockAlert")
	public String listStockAlert() {
		this.setProductStores(this.getProductStoreService()
				.getAllProductStores());
		
		stockAlerts=this.getStockAlertService().getAllStockAlerts();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		criteria.add(Restrictions.eq("isAlert", true));
		this.setPagination(this.getStockAlertService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listProductStore")
	public String listProductStore() {
		
		this.getProductStoreService().saveStockNums(storeRoom);
		 
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				StoreRoom.class);
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getStockAlertService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	
	@Action("listProductStoreRoom")
	public String listProductStoreRoom() {
		List<Integer> ids =this.getProductStoreService().findproductId(codes);
        	DetachedCriteria criteria = this.getPaginationHelper().createQuery(
    				ProductStore.class);
        	criteria.createAlias("product", "p");
			criteria.add(Restrictions.in("p.id", ids));
        	criteria.add(Restrictions.gt("productNum",0.0));
        	criteria.addOrder(Order.desc("recordTime"));
    		this.setPagination(this.getStockAlertService().listByPage(criteria,
    				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("viewStockAlert")
	public String viewStockAlert() {
		stockAlerts = this.getStockAlertService().getStockAlertsByProduct(
				product);
		return SUCCESS;
	}

	@Action("viewProductStore")
	public String viewProductStore() {
		return SUCCESS;
	}

	@Action("listStockAlertByProduct")
	public String listStockAlertByProduct() {
		stockAlerts = this.getStockAlertService().getStockAlertsByProduct(
				product);
		return SUCCESS;
	}

	public String OneStockAlert() {
		stockAlerts = this.getStockAlertService().getOnestockAlert(product,
				storeRoom);
		return SUCCESS;
	}

	public String getCodes() {
		return codes;
	}

	public void setCodes(String codes) {
		this.codes = codes;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public List getNums() {
		return Nums;
	}

	public void setNums(List nums) {
		Nums = nums;
	}

	

	
	
}